package lasec.util;

import java.rmi.registry.LocateRegistry;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import lasec.connect.ConnectionFactory;

import org.apache.mahout.cf.taste.common.TasteException;
import org.apache.mahout.cf.taste.eval.IRStatistics;
import org.apache.mahout.cf.taste.eval.RecommenderBuilder;
import org.apache.mahout.cf.taste.eval.RecommenderEvaluator;
import org.apache.mahout.cf.taste.eval.RecommenderIRStatsEvaluator;
import org.apache.mahout.cf.taste.impl.eval.AverageAbsoluteDifferenceRecommenderEvaluator;
import org.apache.mahout.cf.taste.impl.eval.GenericRecommenderIRStatsEvaluator;
import org.apache.mahout.cf.taste.impl.model.jdbc.MySQLJDBCDataModel;
import org.apache.mahout.cf.taste.model.DataModel;
import org.apache.mahout.cf.taste.recommender.Recommender;

import com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource;

public class RecommenderTest {
	private static void startRegistry() {
		try {
			LocateRegistry.createRegistry(1099);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private static InitialContext createContext() throws NamingException {
		Properties env = new Properties();
		env.put(Context.INITIAL_CONTEXT_FACTORY,
				"com.sun.jndi.rmi.registry.RegistryContextFactory");
		env.put(Context.PROVIDER_URL, "rmi://localhost:1099");
		InitialContext ctx = new InitialContext(env);
		return ctx;
	}

	public static void main(String[] args) throws TasteException {
		startRegistry();
		MysqlConnectionPoolDataSource dataSource = null;
		InitialContext ctx = null;

		try {
			dataSource = new MysqlConnectionPoolDataSource();
			dataSource.setUser("root");
			dataSource.setPassword("1234");
			dataSource.setServerName("localhost");
			dataSource.setPort(3306);
			dataSource.setDatabaseName("mydb");

			ctx = createContext();
			ctx.unbind("mydb");
			ctx.rebind("mydb", dataSource);

		} catch (Exception e) {
			System.out.println("Error while establishing data source");
			e.printStackTrace();
		}

		RecommenderBuilder svdRecommenderBuilder = new RecommenderBuilder() {
			@Override
			public Recommender buildRecommender(DataModel model)
					throws TasteException {
				// TODO Auto-generated method stub
				return new SSVDRecommenderBuilder(model, 0.3);
			}
		};

		final MySQLJDBCDataModel model = new MySQLJDBCDataModel(dataSource,
				"user_movie_rate", "user_id", "movie_id", "movie_rate", "");
		RecommenderIRStatsEvaluator IREvaluator = new GenericRecommenderIRStatsEvaluator();

		IRStatistics evaluation = IREvaluator.evaluate(svdRecommenderBuilder, null, model, null, 2, GenericRecommenderIRStatsEvaluator.CHOOSE_THRESHOLD, 1.0);
		System.out.println(evaluation.getF1Measure());
	}
}